﻿/**
 * 界面控制
 */
var InterfaceCtrl = new Class({
	
	initialize : function(baseArea){
		this.winElmts = new Array();
		this.baseElmt = baseArea || document.body;
	},
	
	/**
	 * 获取一个在遮罩层之上的浮动层窗口
	 * @param {Object} args 浮动层参数
	 */
	getWindow : function(args) {
		args = args || {};
		var zIndex = args.zIndex || this.coverElmt ? (this.coverElmt.getStyle('z-index').toInt() + 1).toString() : '';
		var height = args.height || '';
		var width = args.width || '';
		var top = args.top || height ? (window.getHeight().toInt() - height.toInt())/2/3*2 : 70;
		var left = args.left || width ? (window.getWidth().toInt() - width.toInt())/2 : 100;
		top = top < 0 ? 0 : top;
		left = left < 0 ? 0 : left;
		top = (top + window.getScrollTop()).toString()+'px'
		left = (left + window.getScrollLeft()).toString()+'px'
		var bgColor = args.bgColor || '';
		var classNames = (args['class'] ? args['class']+'-' : '')+'dialogBox';

		var winElmt = null;
		if (!this.windowElmt || args.creatNew) {
			winElmt = new Element('div',{
				'styles': {
					'position' : 'absolute'
				}
			});
			if (!args.show) {
				winElmt.effect('opacity').set(0);
				winElmt.obvious = false;
			}else {
				winElmt.obvious = true;
			}
			winElmt.injectInside(this.baseElmt);
			this.winElmts.push(winElmt);
		}
		winElmt = winElmt || this.windowElmt;
		this.windowElmt = this.windowElmt || winElmt;
		winElmt.setStyles({
			'overflow-y'      : 'auto',
			'background-color': bgColor,
			'z-index'         : zIndex,
			'left'            : left,
			'top'             : top,
			'height'          : height,
			'width'           : width
		});
		winElmt.setProperty('class',classNames);
		$extend(winElmt, {
			show : function() {
				winElmt.obvious = true;
				winElmt.effect('opacity').start(1);
				return winElmt;
			},
			hide : function() {
				winElmt.obvious = false;
				winElmt.effect('opacity').start(0);
				return winElmt;
			},
			close : function() {
				winElmt.hide();
				if (this.allIsHide()) this.closeCover();
				return winElmt;
			}.bind(this),
			clear : function() {
				winElmt.setHTML('');
				return winElmt;
			}
		});
		return winElmt;

	},

	/**
	 * 打开遮罩层, 使处于遮罩层之下的控件无法操作
	 */
	openCover : function() {
		document.documentElement.style.overflow = 'hidden';
		if (!this.coverElmt) {
			this.coverElmt = new Element('div',{
				'styles': {
					'background-color'	: '#333333',
					'position'			: 'absolute',
					'z-index'			: '100',
					'left'				: window.getScrollLeft().toString()+'px',
					'top'				: window.getScrollTop().toString()+'px'
				}
			});
			window.addEvent('resize',function(){
				this.coverElmt.setStyles({
					'height'			: window.getHeight(),
					'width'				: window.getWidth()
				});
			}.bind(this));
		}
		this.coverElmt.setStyles({
			'height'			: window.getHeight(),
			'width'				: window.getWidth()
		});
		this.coverElmt.effect('opacity').set(0);
		this.coverElmt.injectInside(this.baseElmt);
		this.coverElmt.effect('opacity').start(0.3);
	},

	/**
	 * 关闭遮罩层
	 */
	closeCover : function() {
		this.coverElmt.effect('opacity').start(0);
		document.documentElement.style.overflow = '';
	},
	
	/**
	 * 检查是否所有的用于窗口的浮动层都已经隐藏
	 * @return {bool} 所有都隐藏返回True, 否则返回False
	 */
	allIsHide : function(){
		var result = true;
		this.winElmts.each(function(winElmt){
			if(winElmt.obvious) result = false;
		});
		return result;
	},
	
	/**
	 * 关闭所有的浮动层窗口以及遮罩层
	 */
	closeAll : function() {
		this.winElmts.each(function(winElmt){
			winElmt.hide();
		});
		this.closeCover();
	}
});